<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用layui</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<br/>
<br/>
<div class="layui-form" lay-filter="ebookEntryForm"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">问题编号</label>
        <div class="layui-input-inline">
            <input type="text" name="id" class="layui-input" readonly >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-inline">

            <input type="text" name="title" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题介绍</label>
        <div class="layui-input-inline">
            <input type="text" name="detaiDesc" placeholder="请输入" autocomplete="off" class="layui-input" readonly>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">我的回答</label>
        <div class="layui-input-inline">
            <textarea placeholder="请输入内容" name="ansContent" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="*">回答</button>
            <button type="reset" class="layui-btn layui-btn-primary">返回</button>
        </div>
    </div>
    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
</div>
<script src="/layui/layui.js"></script>
<script>
    layui.use(['form','laydate'], function(){
        var form=layui.form;
        var laydate=layui.laydate;
        var $=layui.$;
        var localTest = layui.data('test');//获取表单数据，从主页到弹窗层的数据
        //把获取的值传送到指定位置
        form.val("ebookEntryForm",{
            "id":localTest.id,
            "title":localTest.title,
            // "categoryId":localTest.categoryId,
            "detaiDesc":localTest.detaiDesc,

        });

        // return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        //渲染标签为时间选择框
        laydate.render({
            elem: '#date'
        });
        form.on('submit(*)', function(data){
            //获取表单数据
            var formData= data.field;
            //发送ajax请求到后台
            $.ajax({
                url:"http://localhost:8080/answers/insertanswers",
                data:{
                    id: formData.id,
                    ansContent:formData.ansContent
                },//发送到后端数据
                type:"POST",
                dataType:"JSON",
                success:function (result) {
                    // alert(result.code)
                    //判断
                    if (result.code==0){
                        //添加成功
                        setTimeout(function(){
                            // 关闭当前弹出层
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        },1000)

                        layer.alert("回答已上传")

                    }else {
                        //添加失败
                        layer.alert(result.msg);
                    }
                },
                error:function (result) {
                    layer.alert("服务器爆炸")
                }
            })
            //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });

</script>
</body>
</html>